/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package e.z.blog.back.com.impl.dao;

import e.z.blog.back.bean.State;
import e.z.blog.back.bean.base.Lan;
import e.z.blog.back.com.DaoLanApi;
import java.util.List;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.ioc.Ioc;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

/**
 *
 * @author EnzoZhong
 */
@IocBean
public class DaoLan implements DaoLanApi {

        @Inject( "refer:$Ioc" )
        private Ioc ioc;
        @Inject
        private Dao dao;

        @Override
        public Lan create ( Lan type ) {
                type.setState( State.YELLOW );
                return dao.insert( type );
        }

        @Override
        public Lan read ( Lan type ) {
                Integer lanId = type.getLanId();
                String lanName = type.getLanName();
                Cnd cnd = Cnd.where( "lanId" , "=" , lanId ).or( "lanName" , "=" , lanName );
                return dao.fetch( Lan.class , cnd );
        }

        @Override
        public List<Lan> readAll () {
                return dao.query( Lan.class , null , null );
        }

        @Override
        public Lan update ( Lan type ) {
                type = read( type );
                Lan defLan = ioc.get( Lan.class , "defLan" );
                if ( defLan.getLanName().equalsIgnoreCase( type.getLanName() ) ) {
                        /*
                         * do nothing~
                         */
                } else {
                        dao.update( type );
                }
                return type;
        }

        @Override
        public Lan delete ( Lan type ) {
                type = read( type );
                Lan defLan = ioc.get( Lan.class , "defLan" );
                if ( defLan.getLanName().equalsIgnoreCase( type.getLanName() ) ) {
                        /*
                         * do nothing~
                         */
                } else {
                        switch ( type.getState() ) {
                                case State.BLACK: {
                                        dao.delete( type );
                                        break;
                                }
                                default: {
                                        type.setState( type.getState() + 1 );
                                        dao.update( type );
                                        break;
                                }
                        }
                }
                return type;
        }
}
